#
#   __init__.py
#   journalismManger
#
#   Created by luoxh on 2019-07-17-09:51.
#   Copyright © 2019 luoxh. All rights reserved.
#
from flask import jsonify, request

from .config import ProdConfig
from .extension.application import Application
from .extension.exceptions import CustomException
from .extension.logger import log
from .extension.status import CustomStatus

app = Application(ProdConfig)


@app.errorhandler(CustomStatus.BAD_REQUEST.value)
def BAD_REQUEST(e):
    return jsonify(
        {'code': CustomStatus.BAD_REQUEST.value, 'msg': CustomStatus.BAD_REQUEST.description, 'request': request.method + ' ' + app.get_url_no_param()}), CustomStatus.BAD_REQUEST.value


@app.errorhandler(CustomStatus.UNAUTHORIZED.value)
def UNAUTHORIZED(e):
    return jsonify(
        {'code': CustomStatus.UNAUTHORIZED.value, 'msg': CustomStatus.UNAUTHORIZED.description, 'request': request.method + ' ' + app.get_url_no_param()}), CustomStatus.UNAUTHORIZED.value


@app.errorhandler(CustomStatus.FORBIDDEN.value)
def FORBIDDEN(e):
    return jsonify(
        {'code': CustomStatus.FORBIDDEN.value, 'msg': CustomStatus.FORBIDDEN.description, 'request': request.method + ' ' + app.get_url_no_param()}), CustomStatus.FORBIDDEN.value


@app.errorhandler(CustomStatus.NOT_FOUND.value)
def NOT_FOUND(e):
    return jsonify(
        {'code': CustomStatus.NOT_FOUND.value, 'msg': CustomStatus.NOT_FOUND.description, 'request': request.method + ' ' + app.get_url_no_param()}), CustomStatus.NOT_FOUND.value


@app.errorhandler(CustomStatus.METHOD_NOT_ALLOWED.value)
def METHOD_NOT_ALLOWED(e):
    return jsonify(
        {'code': CustomStatus.METHOD_NOT_ALLOWED.value, 'msg': CustomStatus.METHOD_NOT_ALLOWED.description, 'request': request.method + ' ' + app.get_url_no_param()}), CustomStatus.METHOD_NOT_ALLOWED.value


@app.errorhandler(CustomStatus.TOO_MANY_REQUESTS.value)
def TOO_MANY_REQUESTS(e):
    return jsonify(
        {'code': CustomStatus.TOO_MANY_REQUESTS.value, 'msg': CustomStatus.TOO_MANY_REQUESTS.description, 'request': request.method + ' ' + app.get_url_no_param()}), CustomStatus.TOO_MANY_REQUESTS.value


@app.errorhandler(CustomStatus.INTERNAL_SERVER_ERROR.value)
def INTERNAL_SERVER_ERROR(e):
    return jsonify(
        {'code': CustomStatus.INTERNAL_SERVER_ERROR.value, 'msg': CustomStatus.INTERNAL_SERVER_ERROR.description, 'request': request.method + ' ' + app.get_url_no_param()}), CustomStatus.INTERNAL_SERVER_ERROR.value


from .middleware import *
